import { Component, ViewChild } from '@angular/core';
import { Router } from '@angular/router';
import { DataTableComponent, DialogService, TableWidthConfig } from 'ng-devui';
import { SharedMethodService } from 'src/app/@core/services/Sharedmethod/shared-method.service';
import { ProcessService } from 'src/app/@core/services/process/process.service';
import { TechnologyRouteViewComponent } from './technology-route-view/technology-route-view.component';

@Component({
  selector: 'app-technology-route',
  templateUrl: './technology-route.component.html',
  styleUrls: ['./technology-route.component.scss']
})
export class TechnologyRouteComponent {
  tabledata:any;//表格数据
  pager = {
    total: 20,
    pageIndex: 1,
    pageSize: 20
  };
  checkedIds = [];
  pageSizeOptions = [10, 20, 30, 50, 100];
  msgs: { severity: any; summary: any; content: any; }[];
  config = {
    id: 'Add-Dirctoy-Type',
    maxHeight: '400px',
    width: '400px',
    backdropCloseable: true,
  }//弹窗配置
  SearchTerm:string='';
  @ViewChild(DataTableComponent, { static: true }) datatable: DataTableComponent;
  checkboxList = [];
  allChecked = false;
  halfChecked = false;
  constructor(private Router:Router,private dialogService: DialogService,private SharedMethodService:SharedMethodService,private ProcessService:ProcessService){}
  ngOnInit(): void {
   this.ProcessService.GetTechnologyRoutes(this.SearchTerm,this.pager.pageIndex,this.pager.pageSize).subscribe((data:any)=>{
      this.tabledata=data.items;
      this.pager.total=data.totalCount;
    })//获取工艺路线
  }
  dataTableOptions = {
    columns: [
      {
        field: 'showType',
        header: '工艺路线编码',
        fieldType: 'text'
      },
      {
        field: 'type',
        header: '工艺路线名称',
        fieldType: 'text'
      },
      {
        field: 'enabled',
        header: '备注',
        fieldType: 'text'
      }
    ]
  };//表格数据
  tableWidthConfig: TableWidthConfig[] = [
    {
      field: 'checkbox',
      width: '80px'
    },
    {
      field:'caozuo',
      width:'150px'
    },
    {
      field: 'type',
      width: '250px'
    },
    {
      field: 'enabled',
      width: '150px'
    },
    {
      field: 'enabled',
      width: '150px'
    }
  ];//表格宽度配置
  pageIndexChange($event: any) {
    this.pager.pageIndex=$event;
    this.ProcessService.GetTechnologyRoutes(this.SearchTerm,this.pager.pageIndex,this.pager.pageSize).subscribe((data:any)=>{
      this.tabledata=data.items;
      this.pager.total=data.totalCount;
    })//获取工艺路线
  }//切换页码
  pageSizeChange($event: any){
    this.ProcessService.GetTechnologyRoutes(this.SearchTerm,this.pager.pageIndex,this.pager.pageSize).subscribe((data:any)=>{
      this.tabledata=data.items;
      this.pager.total=data.totalCount;
    })//获取工艺路线
  }//改变每页显示数量
  Delete(id:any,dialogtype?:string){
    const results = this.dialogService.open({
      ...this.config,
      title: '删除',
      content: '确认删除该条工艺路线？',
      dialogtype: dialogtype,
      onClose: () => {
      },
      buttons: [
        {
          cssClass: 'stress',
          text: '确定',
          handler: ($event: Event) => {
              this.ProcessService.DeleteTechnologyRoutes([id]).subscribe((data:any)=>{
                results.modalInstance.hide();
                this.showToast('success','删除成功','工艺路线删除成功')
                this.checkedIds=[];
                this.ProcessService.GetTechnologyRoutes(this.SearchTerm,this.pager.pageIndex,this.pager.pageSize).subscribe((data:any)=>{
                  this.tabledata=data.items;
                  this.pager.total=data.totalCount;
                })//获取工艺路线
              },(error) => {
                if(error.status==404){
                  this.showToast('error', '失败', '服务器连接失败');
                }else{
                  this.showToast('error', '失败', error.error.message);
                }
              }
              )
          }
        },
        {
          id: 'btn-cancel',
          cssClass: 'common',
          text: '取消',
          handler: ($event: Event) => {
            results.modalInstance.hide();
          },
        },
      ],
    });

  }//删除
  Deletes(dialogtype?:string){
    const results = this.dialogService.open({
      ...this.config,
      title: '删除',
      content: '确认删除该条工艺路线？',
      dialogtype: dialogtype,
      onClose: () => {
      },
      buttons: [
        {
          cssClass: 'stress',
          text: '确定',
          handler: ($event: Event) => {
              this.ProcessService.DeleteTechnologyRoutes(this.checkedIds).subscribe((data:any)=>{
                results.modalInstance.hide();
                this.showToast('success','删除成功','工艺路线删除成功')

                this.ProcessService.GetTechnologyRoutes(this.SearchTerm,this.pager.pageIndex,this.pager.pageSize).subscribe((data:any)=>{
                  this.tabledata=data.items;
                  this.pager.total=data.totalCount;
                })//获取工艺路线
              },(error) => {
                if(error.status==404){
                  this.showToast('error', '失败', '服务器连接失败');
                }else{
                  this.showToast('error', '失败', error.error.messgae);
                }
              }
              )
          }
        },
        {
          id: 'btn-cancel',
          cssClass: 'common',
          text: '取消',
          handler: ($event: Event) => {
            results.modalInstance.hide();
          },
        },
      ],
    });

  }//删除
  edit(id:any){
    this.Router.navigate(['/pages/process/technology-route/edit-technology-route',id])
  }//编辑
  filter(){
    this.pager.pageIndex=1;
    this.ProcessService.GetTechnologyRoutes(this.SearchTerm,this.pager.pageIndex,this.pager.pageSize).subscribe((data:any)=>{
      this.tabledata=data.items;
      this.pager.total=data.totalCount;
    })//获取工艺路线
  }//筛选
  reset(){
    this.SearchTerm='';
    this.ProcessService.GetTechnologyRoutes(this.SearchTerm,this.pager.pageIndex,this.pager.pageSize).subscribe((data:any)=>{
      this.tabledata=data.items;
      this.pager.total=data.totalCount;
    })//获取工艺路线
  }//重置
  showToast(type: any,summary:any,content:any) {
    switch (type) {
    default:
      this.msgs = [{ severity: type, summary: summary, content: content }];
    }
  }//提示框
  cleared(){
    this.SharedMethodService.cleared(this.checkedIds,this.tabledata,this.datatable).subscribe((data:any)=>{
      this.checkedIds=data;
    })
  }
  onRowCheckChange(checked: any, rowIndex: any, nestedIndex: any, rowItem: { $checked: any; $halfChecked: boolean; }) {
    this.SharedMethodService.onRowCheckChange(checked, rowIndex, nestedIndex, rowItem,this.datatable);
  }//单选
  getCheckedRows() {
    this.SharedMethodService.getCheckedRows(this.datatable,'technologyRouteId').subscribe((data:any)=>{
      this.checkedIds=data;

    })
  }//获取选中行
  view(technologyRouteId:any,dialogtype?:string){
    const results = this.dialogService.open({
      ...this.config,
      width:'700px',
      title: '工艺详情',
      data:technologyRouteId,
      content: TechnologyRouteViewComponent,
      dialogtype: dialogtype,
      onClose: () => {
      },
      buttons: [
        {
          cssClass: 'stress',
          text: '确定',
          handler: ($event: Event) => {
            results.modalInstance.hide();
          }
        },
        {
          id: 'btn-cancel',
          cssClass: 'common',
          text: '取消',
          handler: ($event: Event) => {
            results.modalInstance.hide();
          },
        },
      ],
    });
  }
}
